import '/common/config/config.dart';

import 'http.dart';


class DioResponse implements Exception{

  DioResponse({
    this.code,
    this.msg,
    this.data,
  });

  /// 自定义code(可根据内部定义方式)
  int? code = ErrorHandle.unknownError;
  /// 消息(例如成功消息文字/错误消息文字)
  String? msg = '未知错误！';
  /// 接口返回的数据
  dynamic data;

  DioResponse.fromJson(dynamic json) {
    code = json[DataKey.code] as int?;
    if(json.containsKey(DataKey.msg)) {
      msg = json[DataKey.msg] as String?;
    }
    if (json.containsKey(DataKey.data)) {
      data = json[DataKey.data];
    }
  }

  DioResponse.catchError(int errCode,String? errMsg) {
    code = errCode;
    msg = errMsg?? '未知错误！';
  }


  Map<String, dynamic> toJson() {
    final json = <String, dynamic>{};
    json[DataKey.code] = code;
    json[DataKey.msg] = msg;
    json[DataKey.data] = data;
    return json;
  }

  // DioResponse.fromJson(Map<String, dynamic> json) {
  //   code = json[DataKey.code] as int?;
  //   if(json.containsKey(DataKey.msg)) {
  //     msg = json[DataKey.msg] as String?;
  //   }
  //   if (json.containsKey(DataKey.data)) {
  //     data = json[DataKey.data];
  //   }
  // }
  //


}
